package conlon.rocketmq.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import conlon.rocketmq.api.DemoMqApi;
import conlon.rocketmq.api.model.DemoMqModel;
import conlon.rocketmq.dao.I8tLocalTransactionDao;
import conlon.rocketmq.entity.I8tLocalTransactionEntity;
import conlon.rocketmq.proxy.ProducerDefaultProxy;
import conlon.rocketmq.service.I8tLocalTransactionService;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author conlon
 * @since 2021-08-20
 */
@Service
public class I8tLocalTransactionServiceImpl extends ServiceImpl<I8tLocalTransactionDao, I8tLocalTransactionEntity> implements I8tLocalTransactionService {


    @Override
    public boolean createI8tTransaction(Message message) {
        I8tLocalTransactionEntity model = new I8tLocalTransactionEntity();
        model.setDeadline(LocalDateTime.now()).setId(UUID.randomUUID().getLeastSignificantBits()).setSendTime(LocalDateTime.now())
                .setUniqKey(message.getTransactionId());
        return this.save(model);
    }

    @Override
    public boolean getUniqKey(String transactionId) {
        return Optional.ofNullable(this.getOne(new QueryWrapper<I8tLocalTransactionEntity>()
                .lambda().eq(I8tLocalTransactionEntity::getUniqKey ,transactionId))).isPresent();
    }
}
